package com.itmuch.cloud.study.entity;

import com.alibaba.fastjson.JSON;
import okhttp3.*;

import java.io.*;
import java.util.*;



/**
 * 
 * @author 齐丹枫
 *
 *
 *
 *
 *
 */

public class TestEntity {


	static MediaType mediaType = MediaType.parse("application/json; chartset=utf-8");
	static OkHttpClient client = new OkHttpClient();
	
	
    public static void post(){
    	 String url="http://120.26.165.102:8888/saveOrderwithEntity";
    	//String   url="http://localhost:8888/saveOrderwithEntity";
    	 for (int i = 1; i <=9; i++) {
    		 Order order=new Order();
    		 List<TransportDetail> transportDetails=new ArrayList<TransportDetail>();
    		 String transportId=UUID.randomUUID().toString();
    		 Transport transport=new Transport();
    		 transport.setId(transportId);
    		 transport.setDelivery("420592756"+i);
    	  	 transport.setPlantCode("5222");
    		 transport.setPlantName("111");
    		 transport.setPoNum("poNumber");
    		 transport.setDocDate("20161206");
    		 transport.setSendDate("20161206");
    		 transport.setDescRoute("descRoute");
    		 transport.setOrderRemark("orderRemark");
    		 transport.setRouteCode("123");
    		 transport.setShipToName("shiperCode");
    		 transport.setShipTo("31302422");
    		 transport.setSoldTo("solderCode");
    		 transport.setSoldToName("asndfiahsad");
    		 transport.setTel("1111000");
    		 transport.setDelAddress("13285322322");
    		 for(int j=0;j<=5;j++){
    			 TransportDetail transportDetail=new TransportDetail();
    			 transportDetail.setId(UUID.randomUUID().toString());
    			transportDetail.setItem("123123");
    			 transportDetail.setCustMaterial("235345345");
    			 
    			 transportDetail.setDeliveryQty(100+"");
    			 transportDetail.setRemark("detailInstruction"+j);
    			 if(i<3){
    			 	//transportDetail.setOrderNo(UUID.randomUUID().toString()+"00");
    				 transportDetail.setOrderNo("11111111111111");
    			 
    			 }else if(i>=3 && i<6){
    			    	transportDetail.setOrderNo("2222222222");
    			 }else{
    				 transportDetail.setOrderNo("333333333333333");
				}
    			 transportDetail.setDescription("materialDescription");
    			 transportDetail.setProductCode("productCode");
    			 transportDetail.setRefItm("refCode");
    			 transportDetail.setMaterial("material"+j);
    			 transportDetail.setUnit("salesUnit"+j);
    			 transportDetail.setLocStorage("storageLocation"+j);
    			 transportDetails.add(transportDetail);
    		 }
    		 order.setTransport(transport);
    		 order.setTransportDetails(transportDetails);
    		 String json = JSON.toJSONString(order);
    		 System.out.println(json);
    		 RequestBody body = RequestBody.create(mediaType, json);
    		 Request request = new Request.Builder()
    				 .url(url)
    				 .post(body)
    				 .build();
    		 Response response;
    		 try {
    			 response = client.newCall(request).execute();
    			 if (response.isSuccessful()) {
    				 System.out.println(response.body().string());
    			 }
    		 } catch (IOException e) {
    			 // TODO Auto-generated catch block
    			 e.printStackTrace();
    		 }
			
		}
       
    }
	
	public static void del(){
		String url = "http://localhost:8888/DeleteDeliverySkubarcode";
		String delivery = "delivery1";
		String material = "material3";
		Map<String, String> map = new HashMap<String, String>();
		map.put("delivery", delivery);
		map.put("material", material);
		String string = JSON.toJSONString(map);
		System.out.println(string);
		RequestBody create = RequestBody.create(mediaType, string);
		Request request = new Request.Builder().url(url).post(create).build();
		try {
			Response response = client.newCall(request).execute();
			if(response.isSuccessful()){
				System.out.println(response.body().string());
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void scanDeliverySkubarcode(){
		String url = "http://localhost:8888/ScanDeliverySkubarcode";
		String delivery = "201702151";
	//	for (int i = 0; i < 10; i++) {
			String barCode = "a";
			Map<String, String> map = new HashMap<String, String>();
			map.put("delivery", delivery);
			map.put("barcode", barCode);
			String string = JSON.toJSONString(map);
			System.out.println(string);
			RequestBody create = RequestBody.create(mediaType, string);
			Request request = new Request.Builder().url(url).post(create).build();
			try {
				Response response = client.newCall(request).execute();
				if(response.isSuccessful()){
					System.out.println(response.body().string());
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		//}
		
	}
	public static void printSkuLabelInfo(){
		String url = "http://localhost:8888/PrintSkuLabelInfo";
		String delivery = "delivery1";
		//for (int i = 0; i < 10; i++) {
			String barCode = "c1";
			Map<String, String> map = new HashMap<String, String>();
			map.put("delivery", delivery);
			map.put("barcode", barCode);
			String string = JSON.toJSONString(map);
			System.out.println(string);
			RequestBody create = RequestBody.create(mediaType, string);
			Request request = new Request.Builder().url(url).post(create).build();
			try {
				Response response = client.newCall(request).execute();
				if(response.isSuccessful()){
					System.out.println(response.body().string());
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	//	}
		
	}
	
	
	public static void bindrelation(){
	//	String url = "http://222.72.139.13:8888/bindrelation";
        String url="";
        File file = new File("F:\\barcode.txt");
        Material itemCode = new Material("14010734", "1","");
        List<Qrcode>  barCodes = new ArrayList<Qrcode>();

        try {
            FileReader fr = new FileReader("F:\\barcode.txt");
            BufferedReader br = new BufferedReader(fr);
            String line = "";
           // String u = "http://qr.huggies.com.cn/q/";
            String r="";
            while ((line = br.readLine())!= null){
               // System.out.println(line.substring(u.length())+);
                r=r+"'"+line+"',";

                barCodes.add(new Qrcode(line));
            }
            System.out.println(r);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


		//String[] arr = {"a","b","c","d","e"};
			/*Material itemCode = new Material("14010734", "1","");
			List<Qrcode>  barCodes = new ArrayList<Qrcode>();
			//for (int j = 1; j <= 10; j++) {
				barCodes.add(new Qrcode("10581708300028899090"));*/
			//}
			MaterialQrCode code = new MaterialQrCode();
			code.setBarCodes(barCodes);
			code.setItemCode(itemCode);
			String string = JSON.toJSONString(code);
			RequestBody create = RequestBody.create(mediaType, string);
			//这里把获得的token放到头部信息中
			Request request = new Request.Builder()
					.addHeader("Authorization", "eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJyb290IiwiYXVkaWVuY2UiOiJ3ZWIiLCJjcmVhdGVkIjoxNDg3OTAxMDQzNzQ4LCJleHAiOjM4MTEzMzM0NjZ9.Xbkbe7chl5z6q7ob-BnO1-lIFouVi-c26iZAjWALQkR-_mfkum0fuC2p2c-cXcPHaEeeMY_EZZPubPQubZ0OTA")
					.url(url).post(create).build();
			try {
				Response response = client.newCall(request).execute();
				if(response.isSuccessful()){
					System.out.println(response.body().string());
					//long end = System.currentTimeMillis();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
			
				
		}
	public static String generalToken(String userName,String passWord){
		 OkHttpClient client = new OkHttpClient();
		  FormBody formBody = new FormBody.Builder()
				  .add("username", userName)
				  .add("passwd", passWord)
				  .build();
		
		  //String credential = Credentials.basic("ehsure@2017", "8BiUk$.)tpJ8wt$Cs[p0Sjv+[*iF^p");
		  Request request = new Request.Builder()
			    //	.header("Authorization", credential)
			        .url("http://115.29.12.30:8050/login")
			        .post(formBody)
			        .build();
		 
		try {
			 Response response = client.newCall(request).execute();
			return response.body().string();
		} catch (IOException e1) {
		e1.printStackTrace();
		return "ERROR";
		}
		
	} 
	
	
	public static String getWarehouseInvoice() {
		  OkHttpClient client = new OkHttpClient();
		  FormBody formBody = new FormBody.Builder()
				  .add("refCertificate", "11111")
				  .add("billDate", "2017-02-22")
				  .add("billCode", "4205927545")
				  .add("orderCode", "JKS0001")
				  .add("submitTime", "2017-02-22 15:01:56")
				//  .add("reFCertificate", "refCertificate")
				  .add("destName", "江苏时代启东店")
				  .add("srcName", "上海金佰利项目仓库仓")
				  .add("srcCode", "5222")
				  .add("destCode", "31302422")
				  .add("boxCodes", "00471620500639070229,00471620500639090136,")
				  .add("soldTo", "soldTo")
				  .build();
		
		  String credential = Credentials.basic("ehsure@2017", "8BiUk$.)tpJ8wt$Cs[p0Sjv+[*iF^p");
		  Request request = new Request.Builder()
			    	.header("Authorization", credential)
			        .url("http://106.14.46.0:8099/api/proj/openapi/rdcoutbill")
			        .post(formBody)
			        .build();
		 
		try {
			 Response response = client.newCall(request).execute();
			return response.body().string();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			  return null;
		}
	}
	
	
	

}
